package com.jayden.scalevideoplayer;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.jayden.scalevideoplayer.videoplayer.MediaPlayerControl;
import com.jayden.scalevideoplayer.videoplayer.VideoPlayerView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private VideoPlayerView mVideoPlayer;
    private MediaPlayerControl mVideoControl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        updateVideoLayoutParams(newConfig.orientation);
    }

    private void initView() {
        mVideoPlayer = findViewById(R.id.video_player);
        TextView mSwitchScreen = findViewById(R.id.switch_screen);
        mSwitchScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int orientation = getResources().getConfiguration().orientation;
                if (orientation == Configuration.ORIENTATION_UNDEFINED
                        || orientation == Configuration.ORIENTATION_PORTRAIT) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                } else {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
            }
        });
        updateVideoLayoutParams(getResources().getConfiguration().orientation);
    }

    private void initData() {
        mVideoControl = new MediaPlayerControl(mVideoPlayer);
        try {
            mVideoControl.setDataSource(getAssets().openFd("video.mp4"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void updateVideoLayoutParams(int orientation) {
        ViewGroup.LayoutParams videoParentParams = mVideoPlayer.getLayoutParams();
        FrameLayout.LayoutParams videoParams = mVideoPlayer.getInternalLayoutParams();
        if (orientation == Configuration.ORIENTATION_UNDEFINED
                || orientation == Configuration.ORIENTATION_PORTRAIT) {
            int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 350, getResources().getDisplayMetrics());
            videoParentParams.height = height;
            videoParams.height = (int) (height * 0.6);
        } else {
            videoParentParams.height = RelativeLayout.LayoutParams.MATCH_PARENT;
            videoParams.height = FrameLayout.LayoutParams.MATCH_PARENT;
        }
    }

}
